﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Utils.MVP;
using RMS.Web.Shell.Presenter;
using RMS.Web.Shell.View;
using RMS.DataAccess.Entities;

namespace RMS
{
    public partial class ManageRadiologyImageView : Page<ManageRadiologyImagePresenter>, IManageRadiologyImageView
    {
        RadiologyImage radiologyImage;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    GetRequest(Convert.ToInt32(Request.QueryString["RID"].ToString().Decrypt(Constants.ApplicationKey)));
                }
                catch (Exception)
                { }
            }
        }

        protected void BtnReturnrequest_Click(object sender, EventArgs e)
        {
            if (TXTRequestid.Text != "")
                GetRequest(Convert.ToInt32(TXTRequestid.Text));
            else if (TXTPatientname.Text != "")
                FillgvRequestsByPName(TXTPatientname.Text);
            else if (TXTPatientid.Text != "")
                FillgvRequestsByPId(Convert.ToInt32(TXTPatientid.Text));
        }
        protected void gvRequests_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = (GridView)sender;
            int RequestID = Convert.ToInt32(gv.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            ButtonAction(e.CommandName, RequestID);
        }
        private void ButtonAction(string command, int RequestID)
        {
            if (command == "GetRequest")
            {
                GetRequest(RequestID);
            }
        }

        protected void BtnAddImage_Click(object sender, EventArgs e)
        {
            radiologyImage = new RadiologyImage();
            string imagePath = Server.MapPath(Constants.UploadFolderPath + Guid.NewGuid()) + ".dcm";
            DicomFileUpload.SaveAs(imagePath);
            radiologyImage.RequestId = Convert.ToInt32(TXTRequestid.Text);
            radiologyImage.ImagePath = imagePath;
            this.Presenter.insertRadiologyImage(radiologyImage);
            Notification.Show();
            imgDicom.ImageUrl = imagePath.ToVirtualPath() + ".Jpg";
            imgDicom.Visible = true;
        }

        private void GetRequest(int RequestID)
        {
            var result = this.Presenter.CurrentRequest(RequestID);
            TXTPatientid.Text = result.PatientID.ToString();
            TXTPatientname.Text = result.FirstName + " " + result.FamilyName;
            TXTRequestid.Text = result.RequestId.ToString();
        }
        private void FillgvRequestsByPId(int PetientId)
        {
            var result = this.Presenter.GetRequest(PetientId);
            gvRequests.DataSource = result.Rows;
            gvRequests.DataBind();
        }
        private void FillgvRequestsByPName(String PetientName)
        {
            var result = this.Presenter.GetRequest(PetientName);
            gvRequests.DataSource = result.Rows;
            gvRequests.DataBind();
        }
    }
}